//
//  NewSongViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "NewSongViewController.h"
#import "firstModel.h"
#import "UIImageView+WebCache.h"
#import <AFNetworking/AFNetworking.h>
#import "MyflowLayout.h"
#import "CollectionViewCell.h"
#import "newSongModel.h"
#import "secondNewsongViewController.h"
#import "SingerModel.h"
#import "MJRefresh.h"
#import "PendulumView.h"
#import "AWCollectionViewDialLayout.h"
#import "firsPageViewController.h"
@interface NewSongViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,retain)UICollectionView *colle;
@property (nonatomic ,retain)NSMutableArray *array;
@property (nonatomic ,retain) AWCollectionViewDialLayout *dialLayout;
@end

@implementation NewSongViewController

{
    NSMutableDictionary *thumbnailCache;
    BOOL showingSettings;
    UIView *settingsView;
    
    UILabel *radiusLabel;
    UISlider *radiusSlider;
    UILabel *angularSpacingLabel;
    UISlider *angularSpacingSlider;
    UILabel *xOffsetLabel;
    UISlider *xOffsetSlider;
    UISegmentedControl *exampleSwitch;
    AWCollectionViewDialLayout *dialLayout;
    
    int type;


}
@synthesize rightSwipeGestureRecognizer;

- (void)viewDidLoad {
    [super viewDidLoad];
   

    [self createRequest];
    UIColor *ballColor = [UIColor colorWithRed:0.47 green:0.60 blue:0.89 alpha:1];
    PendulumView *pendulum = [[PendulumView alloc] initWithFrame:self.view.bounds ballColor:ballColor];
    [self.view addSubview:pendulum];
 
    UIBarButtonItem *button =[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(button:)];
    
    [self.navigationItem setLeftBarButtonItem:button];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
    
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    /* 设置翻页动画 */
    if (sender.direction == UISwipeGestureRecognizerDirectionRight)
    {
        firsPageViewController * one = [[firsPageViewController alloc]init];
        
        [self.navigationController popViewControllerAnimated:YES];
        
        [one release];
    }
}
-(void)button:(id)but
{
    firsPageViewController * one = [[firsPageViewController alloc]init];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    [one release];
    
    
}
-(void)dealloc
{
    [_model release];
    [super dealloc];
    
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        self.array =[NSMutableArray array];
        
        self.i =2;
        
    }
    return self;
}

-(void)createRequest
{
        AFHTTPRequestOperationManager *manger =[AFHTTPRequestOperationManager manager];
        manger.responseSerializer =[AFCompoundResponseSerializer serializer];
        NSString *str =[NSString stringWithFormat:@"http://online.dongting.com/recomm/new_albums?page=1&size=30&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=826886cd55c251bacd39b2d6523c12b126f3c590&idfa=27D84D88-B1C6-4173-B7C5-EE78CD26E7FB&utdid=VVrl99FV5JUDAA1STsAtAkrx&alf=201200&bundle_id=com.ttpod.music"];
        
        [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
        {
            NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *array =[dic objectForKey:@"data"];
            for (NSDictionary *dic in array)
            {
                SingerModel *model =[[SingerModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.array addObject:model];
            }
            
            [self createCollectionView];
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
            
            NSLog(@"%@",error);
        }];
    [self.colle.header endRefreshing];
}
-(void)loctge
{
    AFHTTPRequestOperationManager *manger =[AFHTTPRequestOperationManager manager];
    manger.responseSerializer =[AFCompoundResponseSerializer serializer];
    NSString *str =[NSString stringWithFormat:@"http://online.dongting.com/recomm/new_albums?page=%d&size=30&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=826886cd55c251bacd39b2d6523c12b126f3c590&idfa=27D84D88-B1C6-4173-B7C5-EE78CD26E7FB&utdid=VVrl99FV5JUDAA1STsAtAkrx&alf=201200&bundle_id=com.ttpod.music",self.i];
    
    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
         NSArray *array =[dic objectForKey:@"data"];
         for (NSDictionary *dic in array)
         {
             SingerModel *model =[[SingerModel alloc]init];
             [model setValuesForKeysWithDictionary:dic];
             [self.array addObject:model];
         }
         [self.colle reloadData];
     }
        failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         
         NSLog(@"%@",error);
         
     }];
    
    [self.colle.footer endRefreshing];
    
    self.i++;
}
-(void)createCollectionView
{
    CGFloat radius = radiusSlider.value * 1000;
    CGFloat angularSpacing = angularSpacingSlider.value * 90;
    CGFloat xOffset = xOffsetSlider.value * 330;
    CGFloat cell_width =320;
    CGFloat cell_height = 100;
    dialLayout = [[AWCollectionViewDialLayout alloc] initWithRadius:radius andAngularSpacing:angularSpacing andCellSize:CGSizeMake(cell_width, cell_height) andAlignment:WHEELALIGNMENTCENTER andItemHeight:cell_height andXOffset:xOffset];
    
    [self switchExample];
    
    _colle=[[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:dialLayout] autorelease];
    
    [_colle registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [_colle.header beginRefreshing];
    
    [_colle addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadgart)];
    
    [_colle addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loctge)];
    
    _colle.backgroundColor =[UIColor colorWithRed:0.58 green:0.70 blue:0.89 alpha:1];
    
    _colle.delegate=self;

    _colle.dataSource=self;
    
    [self.view addSubview:_colle];
}

-(void)toggleSettingsView{
    CGRect frame = settingsView.frame;
    if(showingSettings){
      
        frame.origin.y = self.view.frame.size.height;
    }else{
     
        frame.origin.y = 44;
    }
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        settingsView.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
    
    showingSettings = !showingSettings;
}


-(void)switchExample{
    type = (int)exampleSwitch.selectedSegmentIndex;
    CGFloat radius = 0 ,angularSpacing  = 0, xOffset = 0;
    
    if(type == 0){
        [dialLayout setCellSize:CGSizeMake(240, 100)];
        [dialLayout setWheelType:WHEELALIGNMENTLEFT];
        
        radius = 350;
        angularSpacing = 18;
        xOffset =120;
    }else if(type == 1){
        [dialLayout setCellSize:CGSizeMake(260, 50)];
        [dialLayout setWheelType:WHEELALIGNMENTCENTER];
        
        radius = 320;
        angularSpacing = 5;
        xOffset = 124;
    }
    
    [radiusLabel setText:[NSString stringWithFormat:@"Radius: %i", (int)radius]];
    radiusSlider.value = radius/1000;
    [dialLayout setDialRadius:radius];
    
    [angularSpacingLabel setText:[NSString stringWithFormat:@"Angular spacing: %i", (int)angularSpacing]];
    angularSpacingSlider.value = angularSpacing / 90;
    [dialLayout setAngularSpacing:angularSpacing];
    
    [xOffsetLabel setText:[NSString stringWithFormat:@"X offset: %i", (int)xOffset]];
    xOffsetSlider.value = xOffset/320;
    [dialLayout setXOffset:xOffset];
  
}

-(void)loadgart

{
  
    [self.array removeAllObjects];
    
    [self.colle reloadData];
    
    [self createRequest];
    
    [self.colle.header endRefreshing];
    self.i=0;

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    SingerModel  *mode =[self.array objectAtIndex:indexPath.item];
    
    [cell setnewSongModel:mode];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
    
    secondNewsongViewController *second= [[secondNewsongViewController alloc]init];
    SingerModel  *mode =[self.array objectAtIndex:indexPath.item];
    second.model =mode;
    [self.navigationController pushViewController:second animated:YES];
    CATransition *trans = [CATransition animation];
    trans.type = @"push";
    trans.duration = 0.8f;
    trans.subtype = @"fromLeft";
    trans.type=@"rippleEffect";
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind ==UICollectionElementKindSectionHeader) {

        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        return header;
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
